package cn.kgc.springcloud.service;

import cn.kgc.springcloud.entity.Clazz;
import cn.kgc.springcloud.entity.Group;
import cn.kgc.springcloud.feignclient.ClazzClient;
import cn.kgc.springcloud.mapper.GroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/6/17
 */
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private ClazzClient clazzClient;

    @Override
    public List<Group> findAll() {
        List<Group> groups = groupMapper.selectAll();
        //遍历集合 设置clazz属性的值  根据clazzId 查询班级信息  调用班级服务
        groups.forEach(group -> {
            String clazzId = group.getClazzId();
            Clazz clazz = clazzClient.getById(Integer.parseInt(clazzId));
            group.setClazz(clazz);
        });
        return groups;
    }

    @Override
    public Group addGroup(Group group) {
        group.setCreateDate(new Date());
        Integer line =  groupMapper.insertGroup(group);
        return group;
    }

    @Override
    public Group getById(Integer id) {
        return groupMapper.getById(id);
    }
}
